import { queryVerifyState } from '@/services/annotaionAnalysis';
import getApiData, { getCPASTableAipsData, getNestApiData } from '@/services/cpasRequest';
export const queryVerifyStatefs = async (params: any) => {
  const res = await queryVerifyState(params);
  return res;
};

export async function getWscwbbData(params: {
  dbname: string;
  type: string;
  cols: string;
  keys: string;
  filterConditions: string;
}) {
  try {
    const res = await getApiData('account.ws.wscwbb', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: '接口account.ws.wscwbb出错了！',
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口account.ws.wscwbb出错了！',
      success: false,
    });
  }
}

/**
 * 批量清空数据
 */
export const batchClearData = async (params: any) => {
  try {
    const res = await getNestApiData('/cpasTableOperation/batchClearCpasTableData', 'post', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      msg: res.msg,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口batchClearCpasTableData出错了！',
      success: false,
    });
  }
};
/**
 * 批量清空公式
 */
export const batchClearGSData = async (params: any) => {
  try {
    const res = await getNestApiData('/cpasTableOperation/batchClearFormulas', 'post', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      msg: res.msg,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口batchClearFormulas出错了！',
      success: false,
    });
  }
};

/**
 *
 */
export const batchVerifyRecal = async (params: any) => {
  try {
    const res = await getNestApiData('/cpasTableOperation/batchVerifyRecalculate', 'post', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      msg: res.msg,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口saveCpasDesc出错了！',
      success: false,
    });
  }
};

export async function getCpasAllTids(params: {
  dbname: string;
  originTableName: string;
  isAllTid: boolean;
}) {
  try {
    const res = await getApiData('account.design_table.allTids', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口getCpasAllTids出错了！',
      success: false,
    });
  }
}
export async function getYearADate(params: { dbname: string }) {
  try {
    const res = await getApiData('cpasTable.getYearADate', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口getDataList出错了！',
      success: false,
    });
  }
}

/**
 *
 */
export const saveCpasDescList = async (params: any) => {
  try {
    const res = await getNestApiData('/cpasTableEngine/saveCpasDesc', 'post', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口saveCpasDesc出错了！',
      success: false,
    });
  }
};

export const getJudgmentSheetName = async (params: any) => {
  try {
    const res = await getNestApiData('/cpasTableEngine/getJudgmentSheetName', 'post', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: 'false',
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口saveCpasDesc出错了！',
      success: false,
    });
  }
};
/**
 * 获取
 */
export const getCpasByCategory = async (params: any) => {
  try {
    const res = await getNestApiData('/cpasTableEngine/queryCpasDescByCategory', 'post', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口queryCpasDescByCategory出错了！',
      success: false,
    });
  }
};

/**
 * 获取基础报表数据列映射
 */
export const getColumnsMapByReport = async (params: any) => {
  try {
    const res = await getCPASTableAipsData('getColumnsMapByReport', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口getColumnsMapByReport出错了！',
      success: false,
    });
  }
};

/**
 * 获取报表基础数据行映射
 * @param tid
 */
export const getRowsMapByReport = async (params: any) => {
  try {
    const res = await getCPASTableAipsData('getRowsMapByReport', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口getRowsMapByReport出错了！',
      success: false,
    });
  }
};

/**
 * 获取表数据根据name
 */

export const getDesignTableInfoByNameApi = async (params: any) => {
  const res = await getCPASTableAipsData('getCpasTableDataByName', params);
  if (res.ok) {
    return Promise.resolve({
      data: res.data,
      success: true,
    });
  }
  return Promise.reject({
    data: res.data,
    success: false,
  });
};

// 校验结果
export async function gettableIsVerifyTid(params: {
  tableName: string;
  dbname: string;
  isTid: boolean;
}) {
  try {
    const res = await getApiData('account.design_table.isVerifyAndTid', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: '接口account.design_table.isVerify出错了！',
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口account.design_table.isVerify出错了！',
      success: false,
    });
  }
}

export async function getTableperiodAndPointList(params: {
  dbname: string;
  isDesc: boolean;
  reportType: number;
}) {
  try {
    const res = await getApiData('account.timeList.periodAndPoint', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: '接口account.timeList.periodAndPoint出错了！',
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口account.timeList.periodAndPoint出错了！',
      success: false,
    });
  }
}

export async function getPeriodInfo(params: { dbname: string }) {
  try {
    const res = await getApiData('account.getPeriodInfo', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: '接口account.getPeriodInfo出错了！',
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口account.getPeriodInfo出错了！',
      success: false,
    });
  }
}

// 初始化模板
export async function templateInitApi(params: {
  dbName: string;
  templateName: string;
  tableNames: string[];
  type: string;
  dataId?: string;
}) {
  try {
    const res = await getNestApiData('/template/importTemplate', 'post', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.ok,
        msg: res.msg,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.ok,
      msg: res.msg,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口/template/importTemplate,请求失败！',
      success: false,
    });
  }
}

// 初始化模板
export async function saveTemplateApi(params: {
  dbName: string;
  templateName: string;
  tableNames?: string[];
  type: string;
  dataId?: string;
}) {
  try {
    const res = await getNestApiData('/template/exportDataToSqlite', 'post', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      msg: res.msg,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口/template/exportDataToSqlite,请求失败！',
      success: false,
    });
  }
}

// reloadBatchDataRecalculateFs
export async function reloadBatchDataRecalculateFs(params: {
  dbname: string;
  allParameters: any;
  negationRow: string[];
  data_id?: string;
  currentTableIndex?: number;
}) {
  try {
    const res = await getNestApiData('/cpasTableOperation/batchDataRecalculateFs', 'post', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        msg: res.msg,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      msg: res.msg,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口/template/exportDataToSqlite,请求失败！',
      success: false,
    });
  }
}

// CpasTable批量重算
export enum BatchOperationType {
  DATA_RECALCULATE = 'dr', //  数据批量重算
  VERIFY_RECALCULATE = 'vr', // 勾稽重算
  VERIFY_STATISTICS = 'vs', //勾稽重算,带有统计信息
  DATA_VERIFY_RECALCULATE = 'dvr', // 批量重算,勾稽重算组合.不需要勾稽信息
}
export async function reCalculateCpasTable(params: any) {
  try {
    const res = await getNestApiData('/cpasTableOperation/batchDataAndVerifyRecalculate', 'post', {
      ...params,
      type: BatchOperationType.DATA_VERIFY_RECALCULATE,
    });
    return Promise.resolve({
      data: res.data,
      msg: res.msg,
      success: res.ok,
    });
  } catch (error) {
    return Promise.resolve({
      data: error,
      msg: 100000,
      success: false,
    });
  }
}

/**
 * 获取校验数据列映射
 */
export const getVerifyByRMap = async (params: any) => {
  try {
    const res = await getCPASTableAipsData('getVerifyByRMap', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口getVerifyByRMap出错了！',
      success: false,
    });
  }
};